package com.cirkana.viewer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.cirkana.CirkanaActivity;
import com.cirkana.serial.SerializationException;
import com.cirkana.viewer.map.MapActivity;
import com.cirkana.viewer.map.MapPassData;
import com.cirkana.viewer.route.SelectRouteActivity;
import com.cirkana.viewer.route.SelectRoutePassData;
import com.cirkana.viewer.route.model.gpx.Tracker;
import com.cirkana.viewer.security.SignInActivity;
import com.cirkana.viewer.security.SignInPassData;
import com.cirkana.viewer.security.model.Profile;

public class MainActivity extends CirkanaActivity {

	private static final int REQUEST_SIGNIN = 1;
	private static final int REQUEST_SELECTROUTE = 2;
	private static final int REQUEST_MAP = 3;

	private Profile currentProfile;
	private Tracker currentTracker;

	private Button selectedRouteButton;
	private Button startButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		try {
			startActivityForResult(SignInActivity.class, REQUEST_SIGNIN,
					new SignInPassData(this.currentProfile));
		} catch (SerializationException e) {
			openMessageBox("Error", e.getMessage());
		}

		setContentView(R.layout.main);
		this.selectedRouteButton = (Button) findViewById(R.id.main_selected_route);
		this.startButton = (Button) findViewById(R.id.main_start);
		startButton.setEnabled(false);

		this.selectedRouteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					startActivityForResult(SelectRouteActivity.class,
							REQUEST_SELECTROUTE, new SelectRoutePassData(
									currentTracker));
				} catch (SerializationException e) {
					openMessageBox("Error", e.getMessage());
				}
			}

		});

		this.startButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					startActivityForResult(MapActivity.class, REQUEST_MAP,
							new MapPassData(currentTracker));
				} catch (SerializationException e) {
					openMessageBox("Error", e.getMessage());
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {

		case REQUEST_SIGNIN:
			switch (resultCode) {
			case RESULT_OK:
				try {
					SignInPassData d = (SignInPassData) getResponse(data,
							SignInPassData.class);
					this.currentProfile = d.getProfile();
				} catch (SerializationException e) {
					showErrorMessage(e);
				}
				break;
			}
			break;

		case REQUEST_SELECTROUTE:
			switch (resultCode) {
			case SelectRouteActivity.RESULT_SELECTROUTE_OK:
				try {
					SelectRoutePassData d = (SelectRoutePassData) getResponse(
							data, SelectRoutePassData.class);
					this.currentTracker = d.getTracker();
					this.selectedRouteButton.setText(this.currentTracker
							.getFileName());
					startButton.setEnabled(true);
				} catch (SerializationException e) {
					showErrorMessage(e);
				}
				break;
			}
			break;

		case REQUEST_MAP:

		}

	}

}
